// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.ProgressBar)
    jindutiao01: cc.ProgressBar = null;

    @property(cc.Node)
    jindutiao02: cc.Node = null;

    @property(cc.Node)
    jindutiao03: cc.Node = null;

    @property(cc.Node)
    jindutiao04: cc.Node = null;

    gressNum: number = 0;

    jindutiao02bar: cc.Sprite = null;

    jindutiao03bar: cc.Sprite = null;
    jindutiao03mask: cc.Node = null;

    progress4bar: cc.Node = null;
    
    onLoad () {
        this.gressNum = 0;
        if(this.jindutiao01){
            this.jindutiao01.progress = this.gressNum;
        }

        if(this.jindutiao02){
            this.jindutiao02bar = this.jindutiao02.getChildByName('progress2bar').getComponent(cc.Sprite)
            // cc.log(this.jindutiao02bar.fillRange, 'type')
            this.jindutiao02bar.fillRange = 0;
        }

        if(this.jindutiao03){
            this.jindutiao03bar = this.jindutiao03.getChildByName('progress3bar').getComponent(cc.Sprite)
            // cc.log(this.jindutiao02bar.fillRange, 'type')
            this.jindutiao03bar.fillRange = 0;
            this.jindutiao03mask = this.jindutiao03.getChildByName('progressbar3mask')
            cc.log(this.jindutiao03mask, 'this.jindutiao03mask')
            this.jindutiao03mask.active = false;
        }

        if(this.jindutiao04){
            this.progress4bar = this.jindutiao04.getChildByName('progress4bar')
            this.progress4bar.x = -300;
        }
    }

    start () {
        
    }

    update (dt) {
        this.gressNum += dt / 10;
        if(this.jindutiao01){
            if(this.jindutiao01.progress >= 1){
                this.gressNum = 0;
                this.jindutiao03mask.active = false;
                this.jindutiao03bar.node.active = true;
            }
            this.jindutiao01.progress = this.gressNum;
        }

        if(this.jindutiao02bar){
            this.jindutiao02bar.fillRange = this.gressNum;
        }

        if(this.jindutiao03bar){
            this.jindutiao03bar.fillRange = this.gressNum;
            this.jindutiao03mask.getComponent(cc.ProgressBar).progress = this.gressNum;
            if(this.gressNum >= 0.1){
                this.jindutiao03mask.active = true;
                this.jindutiao03bar.node.active = false;
            }
        }

        if(this.progress4bar){
            if(this.progress4bar.x >= 0){
                this.progress4bar.x = -300;
            }
            this.progress4bar.x += 0.5;
        }
    }
}
